﻿/**
 ** @作者:	   王桂龙 QQ731513122
 ** @日期:	   2019.12.6
 ** @简介:     容器控件
 **            实现对所子控件继承于IBHXJZoom接口的，非Dock类的控件进行缩放控制           
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace BHXJDOTNET.NEW
{
    /// <summary>
    /// 继承该类的控件,适合做其他IBHXJZoom类控件的容器
    /// </summary>
    public partial class BHXJContainer : UserControl,IBHXJZOOM,IBHXJCONTAINER
    {
        #region 继承于ZOOM接口的属性和方法
        private Point _OriginPoint = new Point(0, 0);
        /// <summary>
        /// 原点
        /// </summary>
        public Point OriginPoint
        {
            get
            { return this._OriginPoint; }
            set
            {
                if (this._OriginPoint != value)
                {
                    this._OriginPoint = value;
                    if (DesignMode)
                        this.IniLocation = this.BHXJGetIniLocationInDesignMode();
                }
            }
        }
        private bool _AutoOrigin = false;
        /// <summary>
        /// 自动更改原点模式(只在设计模式下有用)
        /// </summary>
        public bool AutoOrigin
        {
            get
            {
                return this._AutoOrigin;
            }
            set
            {
                if (this._AutoOrigin != value)
                {
                    this._AutoOrigin = value;
                    if (DesignMode)
                    {
                        if (this.AutoOrigin)
                        {
                            this.OriginPoint = this.Location;
                        }
                        else
                        {
                            this.OriginPoint = new Point(0, 0);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 初始尺寸
        /// </summary>
        [Browsable(false)]
        public Size IniSize { get; set; }
        /// <summary>
        /// 初始位置
        /// </summary>
        [Browsable(false)]
        public Point IniLocation { get; set; }
        /// <summary>
        /// 初始字体
        /// </summary>
        [Browsable(false)]
        public Font IniFont { get; set; }


        private double _SizeScale = 1;
        /// <summary>
        /// 尺寸比例
        /// </summary>
        [Browsable(false)]
        public double SizeScale 
        {
            get { return this._SizeScale; }
            set 
            {
                if (this._SizeScale != value)
                {
                    this._SizeScale = value;
                }
            }
        }


        private double _LocationScale = 1;
        /// <summary>
        /// 位置比例
        /// </summary>
        [Browsable(false)]
        public double LocationScale
        {
            get { return this._LocationScale; }
            set
            {
                if (this._LocationScale != value)
                {
                    this._LocationScale = value;
                }
            }
        }
        private double _FontScale = 1;
        /// <summary>
        /// 字体比例
        /// </summary>
        [Browsable(false)]
        public double FontScale
        {
            get { return this._FontScale; }
            set
            {
                if (this._FontScale != value)
                {
                    this._FontScale = value;
                }
            }
        }
        #endregion

        #region 内部IZoom控件缩放
        private bool _ZoomFlag = false;
        [Description("是否对子控件启用Zoom功能"), Category("BHXJZoom")]
        /// <summary>
        /// 是否对子控件启用Zoom功能
        /// </summary>
        public bool ZoomFlag
        {
            get { return this._ZoomFlag; }
            set
            {
                if (value != this._ZoomFlag)
                {
                    this._ZoomFlag = value;
                    //重新缩放
                    this.Control_SizeChanged(this, null);
                }
            }
        }

        private bool _ZoomSize = false;
        [Description("（ZoomFlag为真时）缩放子控件尺寸"), Category("BHXJZoom")]
        /// <summary>
        /// （ZoomFlag为真时）缩放子控件尺寸
        /// </summary>
        public bool ZoomSize
        {
            get { return this._ZoomSize; }
            set
            {
                if (value != this._ZoomSize)
                {
                    this._ZoomSize = value;
                    //重新缩放
                    this.Control_SizeChanged(this, null);
                }
            }
        }

        private bool _ZoomFont = false;
        [Description("（ZoomFlag为真时）缩放子控件字体"), Category("BHXJZoom")]
        /// <summary>
        /// （ZoomFlag为真时）缩放子控件字体
        /// </summary>
        public bool ZoomFont
        {
            get { return this._ZoomFont; }
            set
            {
                if (value != this._ZoomFont)
                {
                    this._ZoomFont = value;
                    //重新缩放
                    this.Control_SizeChanged(this, null);
                }
            }
        }

        private bool _ZoomLocation = false;
        [Description("（ZoomFlag为真时）缩放子控件位置"), Category("BHXJZoom")]
        /// <summary>
        /// （ZoomFlag为真时）缩放子控件位置
        /// </summary>
        public bool ZoomLocation
        {
            get { return this._ZoomLocation; }
            set
            {
                if (value != this._ZoomLocation)
                {
                    this._ZoomLocation = value;
                    //重新缩放
                    this.Control_SizeChanged(this, null);
                }
            }
        }
        #endregion

        /// <summary>
        /// 内部尺寸
        /// </summary>
        [Browsable(false)]
        public Size InnerSize
        {
            get;
            set;
        }


        public new AutoScaleMode AutoScaleMode = AutoScaleMode.None;


        Pen edge = new Pen(Color.Transparent);
        /// <summary>
        /// 边框颜色
        /// </summary>
        public Color FrameColor
        {
            get { return edge.Color; }
            set 
            {
                if (this.edge.Color != value)
                {
                    this.edge.Color = value;
                    this.Invalidate(false);
                }
            }
            //get { return this.bhxjPanelTop.BackColor; }
            //set
            //{
            //    if (this.bhxjPanelTop.BackColor != value)
            //    {
            //        this.bhxjPanelTop.BackColor = value;
            //        this.bhxjPanelBottom.BackColor = value;
            //        this.bhxjPanelLeft.BackColor = value;
            //        this.bhxjPanelRight.BackColor = value;
            //    }
            //}
        }

        public BHXJContainer()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            //this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.ResizeRedraw, false);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            #region 设计时,变化事件
            if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
            {
                //设计时事件
                this.SizeChanged += new EventHandler(this.BHXJ_SizeChangedInDesignMode);
                this.FontChanged += new EventHandler(this.BHXJ_FontChangedInDesignMode);
                this.LocationChanged += new EventHandler(this.BHXJ_LocationChangedInDesignMode);

                //innerSize类
                this.SizeChanged += new EventHandler(this.SizeChangedInner);
            }
            #endregion

            //防止字体在设计器中添加该控件时候自动追随父控件
            this.Font = this.Font;
            //缩放模式设置为无
            this.AutoScaleMode = AutoScaleMode.None;

            //设置初始
            
            this.IniSize = this.Size;
            this.IniFont = this.Font;
            this.IniLocation = this.BHXJGetIniLocationInDesignMode();

            this.InnerSize = this.Size;

            //尺寸变化事件
            this.SizeChanged += this.Control_SizeChanged;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (this.edge.Color != Color.Transparent)
            {
                e.Graphics.DrawLine(edge, 0, 0, this.Width - 1, 0);
                e.Graphics.DrawLine(edge, 0, 0, 0, this.Height - 1);
                e.Graphics.DrawLine(edge, 0, this.Height - 1, this.Width - 1, this.Height - 1);
                e.Graphics.DrawLine(edge, this.Width - 1, 0, this.Width - 1, this.Height - 1);
            }
            base.OnPaint(e);
            
        }

        /// <summary>
        /// 尺寸变化事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Control_SizeChanged(object sender, EventArgs e)
        {
            if (this.bhxjPanelTop.DesignMode)
                return;
            bool zoomloaction = this._ZoomLocation;
            bool zoomfont = this._ZoomFont;
            bool zoomsize = this._ZoomSize;
            if (!this._ZoomFlag)
            {
                zoomloaction = false;
                zoomfont = false;
                zoomsize = false;
            }
            Point p = new Point(0, 0);
            float scale = 1;
            if (this.ZoomFlag)
            {
                float inerwith = this.InnerSize.Width;
                float inerhigh = this.InnerSize.Height;
                scale = (((float)this.Width) / inerwith > ((float)this.Height) / inerhigh) ? ((float)this.Height) / inerhigh : ((float)this.Width) / inerwith;
                int x = (int)(((float)this.Width - scale * inerwith) / 2f);
                int y = (int)(((float)this.Height - scale * inerhigh) / 2f);
                p = new Point(x, y);
            }

            if (!(scale > 0))
                return;
            //重置控件原点，并按照比例放大控件
            foreach (Control con in this.Controls)
            {
                if (con.Dock == DockStyle.None)
                {
                    if (con is IBHXJZOOM)
                    {
                        IBHXJZOOM zoom = con as IBHXJZOOM;
                        zoom.OriginPoint = p;
                        if (zoomloaction)
                            zoom.BHXJLocationZoom(scale);
                        else
                            zoom.BHXJLocationZoom(1);
                        if (zoomsize)
                            zoom.BHXJSizeZoom(scale);
                        else
                            zoom.BHXJSizeZoom(1);
                        if (zoomfont)
                            zoom.BHXJFontZoom(scale);
                        else
                            zoom.BHXJFontZoom(1);
                    }
                }
            }

            this.Invalidate(false);
        }

        /// <summary>
        /// 尺寸变化处理函数(设计时用)
        /// </summary>
        /// <param name="bhxjzoom"></param>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SizeChangedInner(object sender, EventArgs e)
        {
            if (this.bhxjPanelTop.DesignMode)
            {
                this.InnerSize = this.Size;
            }
        }
    }
}
